package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.poi.common.usermodel.fonts.FontFamily;
import org.apache.poi.ss.formula.functions.T;
import org.dromara.pdf.fop.core.base.Template;
import org.dromara.pdf.fop.core.doc.Document;
import org.dromara.pdf.fop.core.doc.component.block.BlockContainer;
import org.dromara.pdf.fop.core.doc.component.image.Image;
import org.dromara.pdf.fop.core.doc.component.line.SplitLine;
import org.dromara.pdf.fop.core.doc.component.page.CurrentPageNumber;
import org.dromara.pdf.fop.core.doc.component.page.TotalPageNumber;
import org.dromara.pdf.fop.core.doc.component.table.Table;
import org.dromara.pdf.fop.core.doc.component.table.TableBody;
import org.dromara.pdf.fop.core.doc.component.table.TableCell;
import org.dromara.pdf.fop.core.doc.component.table.TableRow;
import org.dromara.pdf.fop.core.doc.component.text.Text;
import org.dromara.pdf.fop.core.doc.page.Page;
import org.dromara.pdf.fop.core.doc.watermark.Watermark;
import org.dromara.pdf.fop.handler.FontHandler;
import org.dromara.pdf.fop.handler.TemplateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdfServiceImpl implements IPdfService {

    @Autowired
    private IMstnSchemeBatchService mstnSchemeBatchService;
    @Autowired
    private IMstnSchemeComputeService mstnComputeService;

    @Autowired
    private IMstnProjectService mstnProjectService;

    @Autowired
    private IMstnCatalystsService mstnCatalystsService;

    @Autowired
    private IMstnCatalystsChartsService mstnCatalystsChartsService;

    @Autowired
    private IMstnSchemeComputeResultService mstnSchemeComputeResultService;

    private static final String FONT_FAMILY = "STSong";
    private static final String HEADER_HEIGHT = "20pt";
    private static final String PAGE_TOP = "80pt";
    private static final String MARGIN = "30pt";

    private static final String FONT_HEIGHT_15 = "15pt";
    private static final String FONT_HEIGHT_12 = "12pt";

    private static final String LEFT_RIGHT = "50pt";
    @Override
    public void generatePdf(String pdfPath,  Long batchId) {

        List<MstnSchemeCompute> mstnSchemeComputes = mstnComputeService.selectMstnSchemeComputeListByBatchId(batchId);
        MstnSchemeBatch mstnSchemeBatch = mstnSchemeBatchService.selectMstnSchemeBatchByBatchId(batchId);
        MstnProject mstnProject = mstnProjectService.selectMstnProjectByProjectId(mstnSchemeBatch.getProjectId());

        Page firstPage = firstPage(mstnProject);
        Page secondPage = secondPage(mstnSchemeComputes);

        Page thirdPage;
        //是否有计算结果
        MstnSchemeCompute compute = mstnSchemeComputes.get(0);
        MstnSchemeComputeResult searchResult = new  MstnSchemeComputeResult();
        searchResult.setBatchId(mstnSchemeBatch.getBatchId());
        searchResult.setConditionType(compute.getConditionType());
        List<MstnSchemeComputeResult> computeResultList =  mstnSchemeComputeResultService.selectMstnSchemeComputeResultList(searchResult);
        if (computeResultList.isEmpty()){
            Page page = TemplateHandler.Page.build();
            page.setMargin(MARGIN);
            page.setId("thirdPageId");
            page.addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("该标况无匹配的催化剂").setMarginTop(PAGE_TOP)).setMarginLeft(LEFT_RIGHT);
            thirdPage = page;
        } else {
            thirdPage = thirdPage(mstnSchemeBatch, computeResultList);
        }
        Page thridPage2 = thirdPage2();
        Page fourthPage = fourthPage(mstnSchemeBatch, computeResultList, compute);
        //页眉
        addHeadContainer(firstPage,mstnProject.getProjectCode());
        addHeadContainer(secondPage,mstnProject.getProjectCode());
        addHeadContainer(thirdPage,mstnProject.getProjectCode());
        addHeadContainer(thridPage2,mstnProject.getProjectCode());
        addHeadContainer(fourthPage,mstnProject.getProjectCode());
        //页码
//        addPageInfo(page);

        Document document = TemplateHandler.Document.build();
        document.addPage(firstPage)
        .addPage(secondPage)
        .addPage(thirdPage);
        if (!computeResultList.isEmpty()){
            document.addPage(thridPage2);
            document.addPage(fourthPage);
        }
        document.setTitle(mstnProject.getProjectName()).setCreator(mstnSchemeBatch.getCreateBy()).setCreationDate(new Date())
        .transform(pdfPath);

    }


    /**
     * 多标况计算。 暂时不用
     */
    private void addResult(Page page,MstnSchemeBatch mstnSchemeBatch , List<MstnSchemeCompute> mstnSchemeComputes){
        for( MstnSchemeCompute compute : mstnSchemeComputes){
            Text title = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("20pt").setText(compute.getContentByIndex(0)).setMarginTop("20pt");

            Text catalystsTitle = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("20pt").setText("表2.【催化剂选型】").setMarginTop("20pt");
            String smokeFlow = "0".equals(mstnSchemeBatch.getSmokeFlow()) ? "垂直":"水平";
            Text catalystsContent = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setText("烟气" + smokeFlow + "方向流动").setMarginTop("10pt");

            Text resultTitle = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("20pt").setText("表3.【性能保证】").setMarginTop("20pt");
            Text resultContent = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setText("实际运行工况不差于表 1 规定的条件，并且满足以上性能保证的前提要求时，美斯顿公司推荐指导安装的催化剂可以做如下表性能保证：");

            MstnSchemeComputeResult searchResult = new  MstnSchemeComputeResult();
            searchResult.setBatchId(mstnSchemeBatch.getBatchId());
            searchResult.setConditionType(compute.getConditionType());
            List<MstnSchemeComputeResult> computeResultList =  mstnSchemeComputeResultService.selectMstnSchemeComputeResultList(searchResult);
            if (computeResultList.isEmpty()){
                page.addBodyComponent(title).addBodyComponent(
                        TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("该标况无匹配的催化剂").setMarginTop("20pt"));

                continue;
            }
            List<String> catalystsList = Arrays.asList(
                    "设计参数",
                    "催化剂型号",
                    "每台装置催化剂体积, m³",
                    "反应器数",
                    "每台反应器催化剂体积, m³",
                    "每台反应器催化剂层数",
                    "催化剂单元阵列",
                    "每台反应器每层催化剂单元数量",
                    "单元型号",
                    "单元尺寸 (W×H×D), mm",
                    "每层催化剂净高, mm",
                    "反应器尺寸 (L × W ), mm",
                    "每个单元的毛重, kg",
                    "每台反应器内单元数量"
            );
            Table catalystsTable = TemplateHandler.Table.build();
            TableBody catalystsTableBody = TemplateHandler.Table.Body.build();

            // 循环创建表格行
            for (int i = 0; i < catalystsList.size() ; i++) {
                // 创建表格行
                TableRow row = TemplateHandler.Table.Row.build();

                // 创建表格单元格
                TableCell cell = TemplateHandler.Table.Cell.build();
                // 设置单元格边框
                cell.setBorder("1 solid black");
                cell.setWidth("200");
                cell.setFontFamily(FONT_FAMILY);
                if (i == 0) {
                    cell.setHorizontalStyle("center");
                }
                // 创建文本
                Text text = TemplateHandler.Text.build().setText(catalystsList.get(i));
                // 添加文本
                cell.addComponent(text);
                // 添加单元格
                row.addCell(cell);

                // 循环创建表格单元格
                for (int j = 0; j < computeResultList.size(); j++) {
                    // 创建表格单元格
                    TableCell cellContent = TemplateHandler.Table.Cell.build();
                    cellContent.setFontFamily(FONT_FAMILY);
                    // 设置单元格边框
                    cellContent.setBorder("1 solid black");
                    if (i == 0) {
                        cellContent.setHorizontalStyle("center");
                    }
                    // 创建文本
                    Text textContent = TemplateHandler.Text.build().setText(mstnSchemeBatch.getContentByIndex(i,computeResultList.get(j)));
                    // 添加文本
                    cellContent.addComponent(textContent);
                    // 添加单元格
                    row.addCell(cellContent);
                }
                // 添加行
                catalystsTableBody.addRow(row);
            }
            // 设置表格体
            catalystsTable.setBody(catalystsTableBody);


            List<Long> catalystIds = computeResultList.stream().map(MstnSchemeComputeResult::getCatalystsId).collect(Collectors.toList());
            MstnCatalystsCharts queryCatalystsCharts =new MstnCatalystsCharts();
            queryCatalystsCharts.setCatalystsIds(catalystIds);
            queryCatalystsCharts.setXValue(new BigDecimal(mstnSchemeBatch.getLifetime()));
            List<MstnCatalystsCharts> chartsList = mstnCatalystsChartsService.selectMstnCatalystsChartsList(queryCatalystsCharts);


            Table resultTable = TemplateHandler.Table.build();
            TableBody resultTableBody = TemplateHandler.Table.Body.build();
            List<String> resultList = Arrays.asList(
                    "设计参数",
                    "催化剂化学寿命, hours",
                    "出口 NOx, mg/Nm³ dry @3% O2",
                    "氨逃逸率, mg/Nm³ dry @3% O2",
                    "氨消耗量, kg/h",
                    "SO2 氧化率，%",
                    "催化剂层总压降, Pa",
                    "计算号"
            );

            // 循环创建表格行
            for (int i = 0; i < resultList.size() ; i++) {
                // 创建表格行
                TableRow row = TemplateHandler.Table.Row.build();

                // 创建表格单元格
                TableCell cell = TemplateHandler.Table.Cell.build();
                // 设置单元格边框
                cell.setBorder("1 solid black");
                cell.setWidth("200");
                cell.setFontFamily(FONT_FAMILY);
                if (i == 0) {
                    cell.setHorizontalStyle("center");
                }
                // 创建文本
                Text text = TemplateHandler.Text.build().setText(resultList.get(i));
                // 添加文本
                cell.addComponent(text);
                // 添加单元格
                row.addCell(cell);

                // 循环创建表格单元格
                for (int j = 0; j < catalystIds.size(); j++) {
                    // 创建表格单元格
                    TableCell cellContent = TemplateHandler.Table.Cell.build();
                    cellContent.setFontFamily(FONT_FAMILY);
                    // 设置单元格边框
                    cellContent.setBorder("1 solid black");
                    if (i == 0) {
                        cellContent.setHorizontalStyle("center");
                    }
                    // 创建文本
//                    Text textContent = TemplateHandler.Text.build().setText(mstnSchemeBatch.getResultByIndex(i,catalystIds.get(j),chartsList));
                    // 添加文本
//                    cellContent.addComponent(textContent);
                    // 添加单元格
                    row.addCell(cellContent);
                }
                // 添加行
                resultTableBody.addRow(row);
            }
            // 设置表格体
            resultTable.setBody(resultTableBody);

            page.addBodyComponent(title)
                .addBodyComponent(catalystsTitle).addBodyComponent(catalystsContent).addBodyComponent(catalystsTable)
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize("20pt")
                        .setText("性能保证的前提条件：")
                        .setMarginTop("10pt"))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("1.流场"))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("在第一层催化剂入口处烟气与氨气混合充分，且均匀分布于反应器中："))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("-烟气速度的最大相对标准偏差为 15%"))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("-氨气与氮氧化物摩尔比的最大相对标准偏差为 5%"))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("相对标准偏差的定义"))
                    .addBodyComponent(TemplateHandler.Image.build()
                            // 设置图像路径（绝对路径）
                            .setPath("/Users/zhengzuowei/Desktop/3.jpg")
                            // 设置图像宽度
                            .setWidth("300px")
                            // 设置图像高度
//                            .setHeight("150px")
                            // 设置水平居中
//                            .setHorizontalStyle("center")
                            )
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize("15pt").setText("烟气温度比平均值的最大偏差在±10℃范围内"))
                .addBodyComponent(resultTitle).addBodyComponent(resultContent).addBodyComponent(resultTable)
                .addBodyComponent(TemplateHandler.Text.build()
                    .setFontFamily(FONT_FAMILY)
                    .setFontSize("15pt")
                    .setText("-催化剂化学寿命保证期为从第一次通烟气开始后起算。")
                    .setTextIndent("30")
                    .setMarginTop("10pt"))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize("15pt")
                        .setText("-氨气逃逸率的保证值是对锅炉稳定负荷的条件下的平均值而言，当锅炉负荷变化时，氨气逃逸率会发生瞬间较高值。")
                        .setTextIndent("30")
                        .setMarginTop("10pt"))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize("15pt")
                        .setText("-当机组运行偏离表1的设计工况时，催化剂的各项性能需通过性能曲线修正后来保证。")
                        .setTextIndent("30")
                        .setMarginTop("10pt"))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize("15pt")
                        .setText("希望您会对我们的方案感兴趣，并期待着在您方便的时候与您有进一步的交流。")
                        .setTextIndent("30")
                        .setMarginTop("30pt"))
//                .setBodyWatermark(getWatermark())
            ;

        }



    }

    private void addPageInfo(Page page)
    {
        // 创建当前页码
        CurrentPageNumber currentPageNumber = TemplateHandler.CurrentPageNumber.build();
        // 创建总页码
        TotalPageNumber totalPageNumber = TemplateHandler.TotalPageNumber.build().setPageId("pageId");
        // 创建容器
        BlockContainer container = TemplateHandler.BlockContainer.build();
        // 创建当前页码文本
        Text currentText = TemplateHandler.Text.build().setText("当前第： ").setFontFamily(FONT_FAMILY);
        // 创建总页码文本
        Text totalText = TemplateHandler.Text.build().setText("，共： ").setFontFamily(FONT_FAMILY);
        // 添加容器内组件
        container.addComponent(currentText, currentPageNumber, totalText, totalPageNumber);
        // 设置页面主体上边距并添加页面主体组件
        page.setBodyMarginTop("40pt");
    }

    private Page firstPage(MstnProject mstnProject){
        Page page = TemplateHandler.Page.build();
        page.setMargin(MARGIN);
        page.setId("pageId");

        //大标题
        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_15)
                .setText("脱硝催化剂技术方案")
                .setFontWeight("bold")
                .setHorizontalStyle("center")
                .setMarginTop(PAGE_TOP)
        );

        page.addBodyComponent(TemplateHandler.Text.build().
                setFontFamily(FONT_FAMILY).
                setFontSize(FONT_HEIGHT_15)
                .setText("【项目名称】").setMarginTop("40pt").setMarginLeft(LEFT_RIGHT)
        );

        page.addBodyComponent(TemplateHandler.Text.build().
                setFontFamily(FONT_FAMILY).
                setFontSize(FONT_HEIGHT_12)
                .setText(mstnProject.getProjectName())
                .setUnderLineColor("black")
                .enableUnderLine()
                .setMarginTop("20pt").setMarginLeft(LEFT_RIGHT));

        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_15)
                .setText("【询价客户】")
                .setMarginTop("80pt").setMarginLeft(LEFT_RIGHT));

        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_12)
                .setText(mstnProject.getCustomerName())
                .setUnderLineColor("black")
                .enableUnderLine()
                .setMarginTop("20pt").setMarginLeft(LEFT_RIGHT));


        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_15)
                .setText("尊敬的客户:")
                .setMarginTop("80pt").setMarginLeft(LEFT_RIGHT));

        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_15)
                .setText("非常感谢您发来的标题项目的催化剂询价，我们非常荣幸的向您提交催化剂技术方案，希望这会是您感兴趣的方案，并期望贵、我双方能够合作成功!")
                .setTextIndent("30")
                .setLeading("2.5")
                .setMarginTop("10pt")
                .setMarginLeft(LEFT_RIGHT)
                .setMarginRight(LEFT_RIGHT)
        );

        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_15)
                .setText("再次感谢您的支持!有问题请随时与我们联系。")
                .setTextIndent("30")
                .setMarginTop("10pt")
                .setMarginLeft(LEFT_RIGHT)
        );

        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_12)
                .setText("美斯顿（天津）催化剂有限公司")
                .setUnderLineColor("black")
                .enableUnderLine()
                .setHorizontalStyle("right")
                .setMarginTop("100pt")
                .setMarginRight(LEFT_RIGHT)
        );

        Date date = new Date();
        page.addBodyComponent(TemplateHandler.Text.build()
                .setFontFamily(FONT_FAMILY)
                .setFontSize(FONT_HEIGHT_12)
                .setText(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date))
                .setHorizontalStyle("right")
                .setMarginTop("20pt")
                .setMarginRight(LEFT_RIGHT)
        );
        return page;
    }

    private Page secondPage(List<MstnSchemeCompute> mstnSchemeComputes){


        Text title = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_15).setText("表1.【设计基础】").setMarginLeft(LEFT_RIGHT).setMarginTop("40pt");
        Text content = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setText("一套脱硝装置负荷入口烟气量和成分").setFontSize(FONT_HEIGHT_12).setMarginLeft(LEFT_RIGHT).setMarginTop("10pt");
        Page page = TemplateHandler.Page.build();
        page.setMargin(MARGIN);
        page.setId("secondPageId");

        // 创建表格
        Table table = TemplateHandler.Table.build().setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT);
        TableBody tableBody = TemplateHandler.Table.Body.build();

        List<String> titleList = Arrays.asList(
                "设计参数",
                "烟气流量, Nm3/h, wet",
                "烟气温度, °C",
                "烟气压力, mmH2O(G)",
                "NOx, NO2 mg/Nm3@3%O2",
                "NOx present as NO2, %",
                "O2, % Vol wet",
                "H2O, % Vol wet",
                "CO, KG/HR wet",
                "CO2, % Vol wet",
                "SO2, mg/Nm3 dry @3% O2",
                "SO3, mg/Nm3 dry @3% O2",
                "烟气含尘量, mg/Nm3 wet"
        );


        // 循环创建表格行
        for (int i = 0; i < titleList.size() ; i++) {
            // 创建表格行
            TableRow row = TemplateHandler.Table.Row.build();

            // 创建表格单元格
            TableCell cell = TemplateHandler.Table.Cell.build();
            // 设置单元格边框
            cell.setBorder("1 solid black");
            cell.setFontFamily(FONT_FAMILY);
            cell.setFontSize(FONT_HEIGHT_12);
            cell.setWidth("300");
            cell.setHeight("30");
            cell.setVerticalStyle("center");
            if (i == 0) {
                cell.setHorizontalStyle("center");
            }
            // 创建文本
            Text text = TemplateHandler.Text.build().setText(titleList.get(i));
            text.setMarginLeft("5pt");
            // 添加文本
            cell.addComponent(text);
            // 添加单元格
            row.addCell(cell);

            // 循环创建表格单元格
            for (int j = 0; j < mstnSchemeComputes.size(); j++) {
                // 创建表格单元格
                TableCell cellContent = TemplateHandler.Table.Cell.build();
                cellContent.setFontFamily(FONT_FAMILY);
                cellContent.setFontSize(FONT_HEIGHT_12);
                cellContent.setHorizontalStyle("right");
                cellContent.setHeight("30");
                cellContent.setVerticalStyle("center");
                // 设置单元格边框
                cellContent.setBorder("1 solid black");
                // 创建文本
                Text textContent = TemplateHandler.Text.build().setText(mstnSchemeComputes.get(j).getContentByIndex(i));
                textContent.setMarginRight("5pt");
                // 添加文本
                cellContent.addComponent(textContent);
                // 添加单元格
                row.addCell(cellContent);
            }
            // 添加行
            tableBody.addRow(row);
        }
        // 设置表格体
        table.setBody(tableBody);
        table.setMarginTop("10pt");
        //设计基础
        page.addBodyComponent(title).addBodyComponent(content).addBodyComponent(table);

        return page;
    }

    private Page thirdPage(MstnSchemeBatch mstnSchemeBatch , List<MstnSchemeComputeResult> computeResultList ){
        Page page = TemplateHandler.Page.build();
        page.setMargin(MARGIN);
        page.setId("thirdPageId");

        Text catalystsTitle = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_15).setText("表2.【催化剂选型】").setMarginLeft(LEFT_RIGHT).setMarginTop("40pt");
        String smokeFlow = "0".equals(mstnSchemeBatch.getSmokeFlow()) ? "垂直":"水平";
        Text catalystsContent = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setText("烟气" + smokeFlow + "方向流动").setFontSize(FONT_HEIGHT_12).setMarginLeft(LEFT_RIGHT).setMarginTop("10pt");

        List<String> catalystsList = Arrays.asList(
                "设计参数",
                "催化剂型号",
                "每台装置催化剂体积, m³",
                "反应器数",
                "每台反应器催化剂体积, m³",
                "每台反应器催化剂层数",
                "催化剂单元阵列",
                "每台反应器每层催化剂单元数量",
                "单元型号",
                "单元尺寸 (W×H×D), mm",
                "每层催化剂净高, mm",
                "反应器尺寸 (L × W ), mm",
                "每个单元的毛重, kg",
                "每台反应器内单元数量"
        );
        Table catalystsTable = TemplateHandler.Table.build().setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT);
        TableBody catalystsTableBody = TemplateHandler.Table.Body.build();

        // 循环创建表格行
        for (int i = 0; i < catalystsList.size() ; i++) {
            // 创建表格行
            TableRow row = TemplateHandler.Table.Row.build();

            // 创建表格单元格
            TableCell cell = TemplateHandler.Table.Cell.build();
            // 设置单元格边框
            cell.setBorder("1 solid black");
            cell.setWidth("300");
            cell.setFontFamily(FONT_FAMILY);
            cell.setFontSize(FONT_HEIGHT_12);
            cell.setHeight("30");
            cell.setVerticalStyle("center");
            if (i == 0) {
                cell.setHorizontalStyle("center");
            }
            // 创建文本
            Text text = TemplateHandler.Text.build().setText(catalystsList.get(i));
            text.setMarginLeft("5pt");

            // 添加文本
            cell.addComponent(text);
            // 添加单元格
            row.addCell(cell);

            // 循环创建表格单元格
            for (int j = 0; j < computeResultList.size(); j++) {
                // 创建表格单元格
                TableCell cellContent = TemplateHandler.Table.Cell.build();
                cellContent.setFontFamily(FONT_FAMILY);
                cellContent.setFontSize(FONT_HEIGHT_12);
                cellContent.setHorizontalStyle("right");
                cellContent.setHeight("30");
                cellContent.setVerticalStyle("center");
                // 设置单元格边框
                cellContent.setBorder("1 solid black");
                // 创建文本
                Text textContent = TemplateHandler.Text.build().setText(mstnSchemeBatch.getContentByIndex(i,computeResultList.get(j)));
                textContent.setMarginRight("5pt");

                // 添加文本
                cellContent.addComponent(textContent);
                // 添加单元格
                row.addCell(cellContent);
            }
            // 添加行
            catalystsTableBody.addRow(row);
        }
        // 设置表格体
        catalystsTable.setBody(catalystsTableBody);
        catalystsTable.setMarginTop("10pt");


        page.addBodyComponent(catalystsTitle).addBodyComponent(catalystsContent).addBodyComponent(catalystsTable)

//                .setBodyWatermark(getWatermark())
        ;



        return page;
    }

    private Page thirdPage2(){
        Page page = TemplateHandler.Page.build();
        page.setMargin(MARGIN);
        page.setId("thirdPageId2");
        Text resultTitle = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_15).setText("性能保证的前提条件：").setMarginLeft(LEFT_RIGHT).setMarginTop("40pt");

        page.addBodyComponent(resultTitle)
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("1.流场").setMarginLeft(LEFT_RIGHT).setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("在第一层催化剂入口处烟气与氨气混合充分，且均匀分布于反应器中：").setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("-烟气速度的最大相对标准偏差为 15%").setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("-氨气与氮氧化物摩尔比的最大相对标准偏差为 5%").setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("相对标准偏差的定义").setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Image.build()
                                // 设置图像路径（绝对路径）
                                .setPath("/Users/zhengzuowei/Desktop/3.jpg").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT)
                                // 设置图像宽度
                                .setWidth("300px").setMarginTop("10pt")
                        // 设置图像高度
//                            .setHeight("150px")
                        // 设置水平居中
//                            .setHorizontalStyle("center")
                )
                .addBodyComponent(TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("烟气温度比平均值的最大偏差在±10℃范围内").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT).setMarginTop("10pt"));
        return page;
    }
    private Page fourthPage(MstnSchemeBatch mstnSchemeBatch , List<MstnSchemeComputeResult> computeResultList, MstnSchemeCompute mstnSchemeCompute){
        Page page = TemplateHandler.Page.build();
        page.setMargin(MARGIN);
        page.setId("forthPageId");


        Text resultTitle = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_15).setText("【表3.性能保证】").setMarginLeft(LEFT_RIGHT).setMarginTop("40pt");
        Text resultContent = TemplateHandler.Text.build().setFontFamily(FONT_FAMILY).setFontSize(FONT_HEIGHT_12).setText("实际运行工况不差于表 1 规定的条件，并且满足以上性能保证的前提要求时，美斯顿公司推荐指导安装的催化剂可以做如下表性能保证：").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT).setMarginTop("10pt").setLeading("2");


        List<Long> catalystIds = computeResultList.stream().map(MstnSchemeComputeResult::getCatalystsId).collect(Collectors.toList());
        MstnCatalystsCharts queryCatalystsCharts =new MstnCatalystsCharts();
        queryCatalystsCharts.setCatalystsIds(catalystIds);
        queryCatalystsCharts.setXValue(new BigDecimal(mstnSchemeBatch.getLifetime()));
        List<MstnCatalystsCharts> chartsList = mstnCatalystsChartsService.selectMstnCatalystsChartsList(queryCatalystsCharts);


        Table resultTable = TemplateHandler.Table.build().setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT);
        TableBody resultTableBody = TemplateHandler.Table.Body.build();
        List<String> resultList = Arrays.asList(
                "设计参数",
                "催化剂化学寿命, hours",
                "出口 NOx, mg/Nm³ dry @3% O2",
                "氨逃逸率, mg/Nm³ dry @3% O2",
                "氨消耗量, kg/h",
                "SO2 氧化率，%",
                "催化剂层总压降, Pa",
                "计算号"
        );

        // 循环创建表格行
        for (int i = 0; i < resultList.size() ; i++) {
            // 创建表格行
            TableRow row = TemplateHandler.Table.Row.build();

            // 创建表格单元格
            TableCell cell = TemplateHandler.Table.Cell.build();
            // 设置单元格边框
            cell.setBorder("1 solid black");
            cell.setFontFamily(FONT_FAMILY);
            cell.setFontSize(FONT_HEIGHT_12);
            cell.setWidth("300");
            cell.setHeight("30");
            cell.setVerticalStyle("center");
            if (i == 0) {
                cell.setHorizontalStyle("center");
            }
            // 创建文本
            Text text = TemplateHandler.Text.build().setText(resultList.get(i));
            text.setMarginLeft("5pt");

            // 添加文本
            cell.addComponent(text);
            // 添加单元格
            row.addCell(cell);

            // 循环创建表格单元格
            for (int j = 0; j < catalystIds.size(); j++) {
                // 创建表格单元格
                TableCell cellContent = TemplateHandler.Table.Cell.build();
                cellContent.setFontFamily(FONT_FAMILY);
                cellContent.setFontSize(FONT_HEIGHT_12);
                cellContent.setHorizontalStyle("right");
                cellContent.setHeight("30");
                cellContent.setVerticalStyle("center");
                // 设置单元格边框
                cellContent.setBorder("1 solid black");
                // 创建文本
                Text textContent = TemplateHandler.Text.build().setText(mstnSchemeBatch.getResultByIndex(i,catalystIds.get(j),chartsList, mstnSchemeCompute, mstnCatalystsService.selectMstnCatalystsByCatalystsId(catalystIds.get(j))));
                // 添加文本
                cellContent.addComponent(textContent);
                textContent.setMarginRight("5pt");
                // 添加单元格
                row.addCell(cellContent);
            }
            // 添加行
            resultTableBody.addRow(row);
        }
        // 设置表格体
        resultTable.setBody(resultTableBody);
        resultTable.setMarginTop("10pt");
        page.addBodyComponent(resultTitle).addBodyComponent(resultContent).addBodyComponent(resultTable)
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize(FONT_HEIGHT_12)
                        .setText("-催化剂化学寿命保证期为从第一次通烟气开始后起算。")
                        .setTextIndent("30").setLeading("2.5")
                        .setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize(FONT_HEIGHT_12)
                        .setText("-氨气逃逸率的保证值是对锅炉稳定负荷的条件下的平均值而言，当锅炉负荷变化时，氨气逃逸率会发生瞬间较高值。")
                        .setTextIndent("30").setLeading("2.5")
                        .setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize(FONT_HEIGHT_12)
                        .setText("-当机组运行偏离表1的设计工况时，催化剂的各项性能需通过性能曲线修正后来保证。")
                        .setTextIndent("30").setLeading("2.5")
                        .setMarginTop("10pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT))
                .addBodyComponent(TemplateHandler.Text.build()
                        .setFontFamily(FONT_FAMILY)
                        .setFontSize(FONT_HEIGHT_12)
                        .setText("希望您会对我们的方案感兴趣，并期待着在您方便的时候与您有进一步的交流。")
                        .setTextIndent("30").setLeading("2.5")
                        .setMarginTop("30pt").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT));

        return page;

    }

    /** 页眉 */
    private void addHeadContainer(Page page ,String projectName){

        // 创建页眉文本
        Text headerText = TemplateHandler.Text.build().setText(projectName ).setFontFamily(FONT_FAMILY).setFontSize("10pt").setMarginLeft(LEFT_RIGHT);
        Text dateText = TemplateHandler.Text.build().setText(DateUtils.getDate()).setFontFamily(FONT_FAMILY).setFontSize("9pt").setMarginLeft(LEFT_RIGHT);
        // 创建图像
        Image image = TemplateHandler.Image.build().setPath("/Users/zhengzuowei/Desktop/logo_mini.jpg").setHeight("20pt").setMarginRight(LEFT_RIGHT).setHorizontalStyle("right");


        // 创建表格
        Table table = TemplateHandler.Table.build();
        // 创建表格体
        TableBody tableBody = TemplateHandler.Table.Body.build();

        TableRow topRow = TemplateHandler.Table.Row.build();

        // 创建表格单元格
        TableCell leftTopCell = TemplateHandler.Table.Cell.build();
        // 设置单元格边框
        leftTopCell.setBorder("0");
        // 添加文本
        leftTopCell.addComponent(headerText);
        // 添加单元格
        topRow.addCell(leftTopCell);

        // 创建表格单元格
        TableCell rightTopCell = TemplateHandler.Table.Cell.build();
        // 设置单元格边框
        rightTopCell.setBorder("0");
        // 添加图片logo
        rightTopCell.addComponent(image);
        rightTopCell.setRowSpan(2);
        // 添加文本
        topRow.addCell(rightTopCell);

        TableRow bottomRow = TemplateHandler.Table.Row.build();
        // 创建表格单元格
        TableCell leftBottomCell = TemplateHandler.Table.Cell.build();
        // 设置单元格边框
        leftBottomCell.setBorder("0");
        // 添加文本
        leftBottomCell.addComponent(dateText);
        // 添加单元格
        bottomRow.addCell(leftBottomCell);

        TableRow lineRow = TemplateHandler.Table.Row.build();

        // 创建表格单元格
        TableCell lineCell = TemplateHandler.Table.Cell.build();
        // 设置单元格边框
        lineCell.setBorder("0");
        // 分割线
        lineCell.addComponent(TemplateHandler.SplitLine.build().setStyle("solid").setLength("100%").setMarginLeft(LEFT_RIGHT).setMarginRight(LEFT_RIGHT));
        lineCell.setColumnSpan(2);
        // 添加文本
        lineRow.addCell(lineCell);

        // 添加行
        tableBody.addRow(topRow).addRow(bottomRow).addRow(lineRow);
        // 设置表格体
        table.setBody(tableBody);
        page.setHeaderHeight(HEADER_HEIGHT).addHeaderComponent(table);

    }


    //水印
    private Watermark getWatermark(){
        // 创建多行水印
        return TemplateHandler.Watermark.build()
                // 设置水印id
                .setId("test")
                // 设置水印文本
                .setText("美斯顿（天津）催化剂有限公司", "MSTN(TIANJIN)CATALYST CO.,LTD.")
                // 设置水印文本字体大小
                .setFontSize("10pt")
                // 设置水印图片宽度
                .setWidth("400pt")
                // 设置水印图片高度
                .setHeight("200pt")
                // 设置水印显示宽度
                .setShowWidth("200pt")
                //设置旋转角度
                .setRadians("45")
                // 开启文件覆盖
                .enableOverwrite();

    }

}